package cassandrawindow;

import cassandrawindow.UpdateFraudClick;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 *
 * @author ADMIN
 */
public class Main {
    public static final String KEYSPACE                             =   "AdsData";
    public static final String ColumFamly                           =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf         =   "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf          =   "BannerViewCountCf";
    public static final String ColumFamlyCampaignClickCountCf       =   "CampaignCountCf";
    public static final String ColumFamlyCampaignViewCountCf        =   "CampaignViewCountCf";
    public static final String ColumFamlyCampaignIPCountCf          =   "CampaignIpCountCf";
    public static long checkDateDomainDate = 0;
    
    public static void main(String[] args) throws Exception {        
        //live budget        
        HandleBalanceAndPromotionMobile cpcMobileHandler = new HandleBalanceAndPromotionMobile();
        cpcMobileHandler.handleData();
        
        /*while(true)
        {                           
            //chay live budget
            //CONSTANT.Date ="2013-03-16";
            HandleBalanceAndPromotionCPM cpmHandler = new HandleBalanceAndPromotionCPM();
            cpmHandler.handleData();
            //tinh cpc livebudget
            HandleBalanceAndPromotionCPC cpcHandler = new HandleBalanceAndPromotionCPC();
            cpcHandler.handleData();
            HandleBalanceAndPromotionCPCRemain cpcHandler1 = new HandleBalanceAndPromotionCPCRemain();
            cpcHandler1.handleData();
            HandleBalanceAndPromotionCPCPlus cpcPlusHandler = new HandleBalanceAndPromotionCPCPlus();
            cpcPlusHandler.handleData();                
            HandleBalanceAndPromotionMobile cpcMobileHandler = new HandleBalanceAndPromotionMobile();
            cpcMobileHandler.handleData();                
            HandleBalanceAndPromotionSponsorspot sponsorHandler = new HandleBalanceAndPromotionSponsorspot();
            sponsorHandler.handleData();

            //long last_time = System.currentTimeMillis();
            Debug.println("successfully livebudget");            
            //SLEEP 5 phut
            Thread.sleep(5*60*1000);
        }*/
    }
    
    /*
     * Lay thoi gian tu 6h sang ngay hom truoc toi 5h59 sang ngay hom sau
     * Coi la 1 ngay
     */
    public static String handleDate()
    {
        String retDate = "";//2012-07-24
        String date = TimeUtil.getCurrentTimeInStringForLog();//yyyy/MM/dd H:mm:ss

        String[] arrTime        = new String[2];
        String[] firstArrTime   = new String[3];
        String[] secondArrTime  = new String[3];

        arrTime = date.split(" ");
        firstArrTime    = arrTime[1].split(":");
        secondArrTime   = arrTime[0].split("-");
        if(Integer.valueOf(firstArrTime[0]) >= 7 && Integer.valueOf(firstArrTime[0]) <= 23)
        {
            retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+secondArrTime[2];
        }
        //init checkDate if trigger from 0h -> 6h
        else if (Integer.valueOf(firstArrTime[0]) >= 0 && Integer.valueOf(firstArrTime[0]) < 7 && TimeUtil.getCurrentTime() >= checkDateDomainDate)
        {
            //Debug.println(date + "_" + TimeUtil.getCurrentTime() + "_" + checkDateDomainDate);
            int day = Integer.valueOf(secondArrTime[2]) - 1;
            if(day < 10){
                retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+"0"+String.valueOf(day);
            } else{
                retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+String.valueOf(day);
            }
        }
        return retDate;
    }
    
    public static String getIntervalTime(String firstTime,String lastTime)
    {            
        StringBuilder sb = new StringBuilder();
        int hourFist = 0;
        int hourLast = 0;

        int dayFist = 0;
        int dayLast = 0;

        String[] arrData = new String[2];
        String[] arrHour = new String[2];
        //tinh ngay bat dau
        arrData = firstTime.split(" ");
        dayFist = Integer.valueOf(arrData[0].split("-")[2]);
        //Tinh gio bat dau
        hourFist = Integer.valueOf(arrData[1].split(":")[0]);
        //prefix first
        String prefixFirst = arrData[0].split("-")[0] +"-" +arrData[0].split("-")[1] +"-";
        //tinh ngay ket thuc
        arrData = lastTime.split(" ");
        dayLast = Integer.valueOf(arrData[0].split("-")[2]);
        //Tinh gio ket thuc
        hourLast = Integer.valueOf(arrData[1].split(":")[0]);
        //prefix last
        String prefixLast = arrData[0].split("-")[0] +"-" +arrData[0].split("-")[1] +"-";

        for(int day = dayFist ; day <=dayLast ; day++)
        {
            //Lay ngay theo tung phut neu chi tinh trong ngay
            if((day == dayFist)&&(day == dayLast)){

                for(int hour = hourFist ;hour < hourLast ;hour ++)
                {
                    if(hour < 10)
                    {
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " +"0"+ hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " +"0"+ hour +":" +i).append("'").append(",");
                            }
                        }
                    }else{
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " + hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " + hour +":" +i).append("'").append(",");
                            }
                        }
                    }
                }
                break;
            }
            //Neu trong nhieu ngay thi dau tien la tinh ngay theo tung phut bat dau tu gio dau tien
            if(day == dayFist)
            {
                for(int hour = hourFist ;hour < 24 ;hour ++)
                {
                    if(hour < 10)
                    {
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " +"0"+ hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " +"0"+ hour +":" +i).append("'").append(",");
                            }
                        }
                    }else{
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " + hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " + hour +":" +i).append("'").append(",");
                            }
                        }
                    }
                }
            }
            //Tiep den la ngay cuoi cung
            else if(day == dayLast){
                for(int hour = 0 ;hour < hourLast ;hour ++)
                {
                    if(hour < 10)
                    {
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " +"0"+ hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " +"0"+ hour +":" +i).append("'").append(",");
                            }
                        }
                    }else{
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " + hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " + hour +":" +i).append("'").append(",");
                            }
                        }
                        
                    }
                }
            }
            //Tiep den la tinh cac ngay 
            else{
                for(int hour = 0 ;hour < 24 ;hour ++)
                {
                    if(hour < 10)
                    {
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " +"0"+ hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " +"0"+ hour +":" +i).append("'").append(",");
                            }
                        }
                    } else{
                        for(int i = 0 ;i < 60 ;i++)
                        {
                            if(i < 10)
                            {
                                sb.append("'").append(prefixLast + day + " " + hour +":0" +i).append("'").append(",");
                            }
                            else if((day == dayLast)&&(hour == (hourLast-1))&&(i==59)){
                                sb.append("'").append(prefixLast).append(day).append(" ").append(hour).append(":").append(i).append("'");
                                break;
                             }
                            else{
                                sb.append("'").append(prefixLast + day +" " + hour +":" +i).append("'").append(",");
                            }
                        }
                    }
                }
            }
        }
        return sb.toString();

    }
    public static String getClickInDay(CassandraAction cassAct,String day,String key,String columnFamily) throws ParseException
    {

       StringBuilder sb = new StringBuilder();
       
       for(int i = 0 ; i < 24 ;i++)
       {
           if(i < 10)
           {
                String date = "0"+i+":00:00 "+ day;
                long start_time = TimeUtil.longToDateWithHour(date);
                //them 1h 
                long finish_time = start_time + 60*60*1000;
                long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                sb.append(i).append("h:").append(countClick).append(",");
           } else {
                String date = i+":00:00 " + day;
                 long start_time = TimeUtil.longToDateWithHour(date);
                //them 1h
                long finish_time = start_time + 60*60*1000;
                long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                if(i != 23)
                {
                    sb.append(i).append("h:").append(countClick).append(",");
                } else{
                    sb.append(i).append("h:").append(countClick);
                }
           }
       }
       return sb.toString();
    }            
    /** List order maintained **/
    public static void removeDuplicateWithOrder(ArrayList arlList)
    {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = arlList.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (set.add(element))
            newList.add(element);
        }
        arlList.clear();
        arlList.addAll(newList);
    }

    public static void getDataCampaignAndBannerID(MysqlDataManager mysqldatamanager,CassandraAction cassAct,String startDay,String finishDay,String camId,String banId,String columnFamilyClick,String columnFamilyView) throws ParseException
    {
        String[] arrfirst = new String[3];
        String[] arrlast = new String[3];
        String dayStart = startDay.split("/")[0];
        String dayLast = finishDay.split("/")[0];
        String prefix = "/" +startDay.split("/")[1]+"/"+startDay.split("/")[2];

        String prefixDB = startDay.split("/")[2] + "-" + startDay.split("/")[1] + "-";

        for(int i = Integer.valueOf(dayStart) ; i <= Integer.valueOf(dayLast) ;i++)
        {
            String date = "00:00:00 " + String.valueOf(i) + prefix ;
            long start_time = TimeUtil.longToDateWithHour(date);
            //them 1h
            long finish_time = start_time + 24*60*60*1000;
            long countClick = cassAct.getCountClickByIntervalTime(banId, columnFamilyClick, start_time, finish_time);
            long countView = cassAct.getCountClickByIntervalTime(banId, columnFamilyView, start_time, finish_time);
            mysqldatamanager.insertDataCampaignAndBanner(String.valueOf(camId),String.valueOf(banId), String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
        }

    }
}
